package pub.wkq.resource.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import pub.wkq.api.mq.RabbitMQSMSConfig;
import pub.wkq.common.utils.MailUtils;
import pub.wkq.common.utils.SerializationUtils;
import pub.wkq.mq.SMSContentQO;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

/**
 * 短信监听消费者
 */
@Slf4j
@Component
public class RabbitMQSMSaConsumer {
    @Resource
    private MailUtils mailUtils;

    /**
     * 手动确认监听短信消息，通过 channel 进行手动的确认
     *
     * @param message 消息
     */
    @RabbitListener(queues = {RabbitMQSMSConfig.SMS_QUEUE_NAME})
    public void receiveSMSaMessage(Message message, Channel channel) {
        try {
            //获取消息体
            byte[] body = message.getBody();
            //获取消息体的字符串
            String payload = new String(body);
            log.info("接收到消息：payload: {} : message : {}", payload, message);
            String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
            log.info("接收到消息：routingKey: {}", receivedRoutingKey);

            if (Objects.equals(receivedRoutingKey, RabbitMQSMSConfig.SEND_SMS_ROUTING_KEY)) {
                log.info("接收到消息：发送短信");
            } else if (Objects.equals(receivedRoutingKey, RabbitMQSMSConfig.SEND_EMAIL_ROUTING_KEY)) {
                log.info("接收到消息：发送邮件");
                //此处为邮件发送的消息发送处理逻辑
                SMSContentQO smsContentQO = SerializationUtils.deserializeObject(payload, SMSContentQO.class);
                mailUtils.sendSimpleMail("2112954786@qq.com", smsContentQO.getMobile(), "登录验证码", "您的登录验证码为：" + smsContentQO.getContent() + "，请勿泄露给他人。");
                //手动确认消息
                /**
                 * 参数一：消息的唯一标识
                 * 参数二：是否批量确认，true：批量确认所有小于当前消息唯一标识的消息
                 *       false：只确认当前消息
                 *       如果为true，会将当前消息之前的所有消息都确认
                 */
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            }
        } catch (Exception e) {
            log.error("接收消息发生异常：{}", e.getMessage());
            try {
                /**
                 * 参数一：消息的唯一标识
                 * 参数二：是否批量确认，true：批量确认所有小于当前消息唯一标识的消息
                 *      false：只确认当前消息
                 *      如果为true，会将当前消息之前的所有消息都确认
                 * 参数三：是否重回队列，true：重回队列，false：不重回队列,丢弃消息
                 */
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
//    /**
//     * 监听短信消息
//     *
//     * @param payload 消息体
//     * @param message 消息
//     */
//    @RabbitListener(queues = {RabbitMQSMSConfig.SMS_QUEUE_NAME})
//    public void receiveSMSaMessage(String payload, Message message) {
//        log.info("接收到消息：payload: {} : message : {}", payload, message);
//        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
//        log.info("接收到消息：routingKey: {}", receivedRoutingKey);
//
//        if (Objects.equals(receivedRoutingKey, RabbitMQSMSConfig.SEND_SMS_ROUTING_KEY)) {
//            log.info("接收到消息：发送短信");
//        } else if (Objects.equals(receivedRoutingKey, RabbitMQSMSConfig.SEND_EMAIL_ROUTING_KEY)) {
//            log.info("接收到消息：发送邮件");
//            //此处为邮件发送的消息发送处理逻辑
//            SMSContentQO smsContentQO = SerializationUtils.deserializeObject(payload, SMSContentQO.class);
//            mailUtils.sendSimpleMail("2112954786@qq.com", smsContentQO.getMobile(), "登录验证码", "您的登录验证码为：" + smsContentQO.getContent()+"，请勿泄露给他人。");
//        } else {
//            log.info("接收到消息：未知消息");
//        }
//    }
}
